﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;

namespace TakeoutMangesWeb
{
    public partial class AdminAddProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Product pd = new Product();
            pd.ProductName = txtProductName.Text.Trim();
            pd.ProductPrice = txtProductPrice.Text.Trim();
            pd.ProductSales = txtProductSales.Text.Trim();
            pd.CategoryID =Convert.ToInt32( txtCategory.Text.Trim());
            pd.EnterpriseID = Convert.ToInt32(txtEnterprise.Text.Trim());
            pd.Isdelete = 0;
            if (FileUpload1.HasFile)//判断是否已选择文件
            {
                string path = Server.MapPath("~/img/");//要存储图片的路径
                if (!Directory.Exists(path))//判断该文件夹是否存在，如果不存在就创建
                {
                    Directory.CreateDirectory(path);//创建文件夹
                }
                string extension = Path.GetExtension(FileUpload1.FileName);//扩展名
                string newname = Guid.NewGuid().ToString();//创建新的图片名称
                string name=newname + extension;//名称+后缀组成新文件
                FileUpload1.SaveAs(path + name);//将组成的新图片保存在指定的路径中
                pd.ProductImage = Image1.ImageUrl = "~/img/" + name;//上传成功显示图片
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('请选择你要上传的图片！')</script>");
            }
            int r = TakeoutMangesBLL.ProductBLL.AddProduct(pd);
            if (r>0)
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('添加成功！');location.href='AdminProductInfo.aspx';</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('添加失败！')</script>");
            }
        }
    }
}